﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContestMeter.Common;
using Microsoft.Practices.Unity;
using System.Windows.Input;
using ContestMeter.UI;

namespace ContestMeter.Teacher.UI
{
    public class ConfigurationEditorViewModel : NotifyPropertyChanged
    {
        [Dependency]
        public IConfiguration Configuration { get; set; }

        #region Save Command
        private DelegateCommand<object> _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand<object>(x => Save(), x => CanSave());
                }
                return _saveCommand;
            }
        }
        protected virtual void SaveCanExecuteChanged()
        {
            if (_saveCommand != null)
            {
                _saveCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion

        #region Add Command
        private DelegateCommand<object> _addCommand;
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand<object>(x => Add(), x => true);
                }
                return _addCommand;
            }
        }
        protected virtual void AddCanExecuteChanged()
        {
            if (_addCommand != null)
            {
                _addCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion
        protected virtual void Add()
        {
            Configuration.AddNewTask();
        }

        #region Delete Command
        private DelegateCommand<Task> _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand<Task>(x => Delete(x), x => true);
                }
                return _deleteCommand;
            }
        }
        protected virtual void DeleteCanExecuteChanged()
        {
            if (_deleteCommand != null)
            {
                _deleteCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion
        protected virtual void Delete(Task task)
        {
            Configuration.RemoveTask(task);
        }

        protected virtual void Save()
        {
            Configuration.Save();
        }
        protected virtual bool CanSave()
        {
            return true;
        }

        #region AddTool Command
        private DelegateCommand<object> _addtoolCommand;
        public ICommand AddToolCommand
        {
            get
            {
                if (_addtoolCommand == null)
                {
                    _addtoolCommand = new DelegateCommand<object>(x => AddTool(), x => CanAddTool());
                }
                return _addtoolCommand;
            }
        }
        protected virtual void AddToolCanExecuteChanged()
        {
            if (_addtoolCommand != null)
            {
                _addtoolCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion
        protected virtual void AddTool()
        {
            Configuration.AddNewTool();
        }
        protected virtual bool CanAddTool()
        {
            return true;
        }

        #region RemoveTool Command
        private DelegateCommand<DeveloperTool> _removetoolCommand;
        public ICommand RemoveToolCommand
        {
            get
            {
                if (_removetoolCommand == null)
                {
                    _removetoolCommand = new DelegateCommand<DeveloperTool>(x => RemoveTool(x), x => true);
                }
                return _removetoolCommand;
            }
        }
        protected virtual void RemoveToolCanExecuteChanged()
        {
            if (_removetoolCommand != null)
            {
                _removetoolCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion
        protected virtual void RemoveTool(DeveloperTool tool)
        {
            Configuration.RemoveTool(tool);
        }


    }
}
